package org.llp.org.service.impl;

import org.llp.basic.service.IBaseService;
import org.llp.basic.service.impl.BaseServiceImpl;
import org.llp.basic.utils.PageList;
import org.llp.org.domain.Department;
import org.llp.org.mapper.DepartmentMapper;
import org.llp.org.query.DepartmentQuery;
import org.llp.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void add(Department department){
        department.setCreate_time(new Date());
        super.add(department);
        //拼接path
        Long id = department.getId();
        String path = "";
        if(department.getParent().getId() == null){
            path = "/"+id;
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            path = parent.getPath()+"/"+id;
        }
        department.setPath(path);
        super.update(department);
    }

    @Override
    public void update(Department department){
        department.setCreate_time(new Date());
        super.update(department);
        //拼接path
        String path = "";
        Long id = department.getId();
        if(department.getParent().getId() == null){
            path = "/"+id;
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            path = parent.getPath()+"/"+id;
        }
        department.setPath(path);
        super.update(department);
    }


    @Override
    public List<Department> getDepartmentTree() {
        return departmentMapper.loadTree();
    }
}
